package com.inxedu.os.edu.controller.user;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.inxedu.os.edu.pojo.SysLoginLog;
import com.inxedu.os.edu.pojo.SysRole;
import com.inxedu.os.edu.pojo.SysUser;
import com.inxedu.os.edu.pojo.result;
import com.inxedu.os.edu.service.user.AdminSysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("admin")
@Api(value = "用户接口", tags = "用户管理Controller")
public class AdminUserController {
    @Autowired
    private AdminSysUser adminSysUser;
    @InitBinder({"sysUser"})
    public void initBinderSysFunction(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysUser.");
    }
    //查找角色列表
    @RequestMapping(value = "/sysuser/userlist",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiImplicitParam(name = "keyword",value = "查找用户或邮箱",required = true,dataType = "keyword")
    @ApiOperation(value = "查找用户列表信息",notes = "查找角色列表")
    public  String tosysuser(Model model ,@ModelAttribute("keyWord") String keyWord){
        SysUser user=new SysUser();
        user.setLoginName(keyWord);
        user.setEmail(keyWord);
        List<SysUser> select = adminSysUser.select(user);
        model.addAttribute("userList",select);
        List<SysRole> sysRoles = adminSysUser.selectRole();
        model.addAttribute("sysRoleList",sysRoles);
        return "view/inxedu/admin/system/sys-user-list";
    }
    //伪删除
    @RequestMapping(value = "/sysuser/disableOrstart/{userId}/{type}",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "userId"),
            @ApiImplicitParam(name = "type",value = "用户状态",required = true,dataType = "type")
    })
    @ApiOperation(value = "根据Id修改状态",notes = "0 正常 1冻结 2删除")
    @ResponseBody
    public result updatesysuser(Model model, @PathVariable("userId")int userId, @PathVariable("type") int type) {
        adminSysUser.updatestatus(userId,type);
        result result=new result();
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "/sysuser/updateuser",method = RequestMethod.POST)
    @ApiImplicitParam(name = "sysUser",value = "用户",dataType = "sysUser")
    @ApiOperation(value = "修改成功",notes = "修改成功")
    @ResponseBody
    public result updatausers(Model model, @ModelAttribute("sysUser") SysUser sysUser) {
        adminSysUser.updatauser(sysUser);
        result result = new result();
        result.setSuccess(true);
        result.setMessage("修改成功");
        return result;
    }

    //跳转日志
    @RequestMapping(value = "/sysuser/looklog/{userId}",method = RequestMethod.GET)
    @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "userId")
    @ApiOperation(value = "跳转至日志页面",notes = "查找日志信息")
    public String tolooklog(@PathVariable("userId") Integer userId,Model model){
        System.out.println(userId);
        List<SysLoginLog> sysLoginLogs = adminSysUser.selectByExample(userId);
        System.out.println(sysLoginLogs);
        model.addAttribute("logList",sysLoginLogs);
        return "view/inxedu/admin/system/sys-user-loginlog";
    }
    //修改
    @RequestMapping(value = "/sysuser/initupdateuser/{userId}",method = RequestMethod.POST)
    @ApiImplicitParam(name = "userId",value = "角色id",required = true,dataType = "userId")
    @ApiOperation(value = "修改角色信息",notes = "修改角色信息")
    @ResponseBody
    public result update(Model model,@ModelAttribute("userId")int userId){
        SysUser user = adminSysUser.selectAll(userId);
        System.out.println(user);

//      adminSysUser.updatauser(user1);
        result result=new result();
        result.setSuccess(true);
        result.setEntity(user);
        return result;
    }
    //修改密码
    @RequestMapping(value = "/sysuser/updatepwd/{userId}",method = RequestMethod.POST)
    @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "userId")
    @ApiOperation(value = "修改角色密码",notes = "修改密码")
    @ResponseBody
    public result updadapwd(HttpServletRequest request, @PathVariable("userId") int userId) {
        String newPwd = request.getParameter("newPwd");
        adminSysUser.updadauserpwd(userId, newPwd);
        result result = new result();
        result.setSuccess(true);
        result.setMessage("修改成功");
        return result;
    }
    //新增
    @RequestMapping(value = "/sysuser/createuser",method = RequestMethod.POST)
    @ApiImplicitParam(name = "sysUser",value = "用户",dataType = "sysUser")
    @ApiOperation(value = "创建用户",notes = "创建用户")
    @ResponseBody
    public result insetsysuser(Model model, @ModelAttribute("sysUser") SysUser sysUser) {
        System.out.println(sysUser);
        adminSysUser.insetsysuser(sysUser);
        result result = new result();
        result.setSuccess(true);
        result.setMessage("创建成功");
        return result;
}
}
